//
//  UIButton+JYButtonStatus.m
//  CrowdFunding
//
//  Created by 欧洪飞 on 2017/3/29.
//  Copyright © 2017年 qyc. All rights reserved.
//

#import "UIButton+JYButtonStatus.h"


@implementation UIButton (JYButtonStatus)


/**倒计时 60秒*/
-(void)creatTimer {
    __block int timeout = 59;
    dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, global);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    __weak typeof(self)weakSelf = self;
    dispatch_source_set_event_handler(timer, ^{
        timeout--;
        if (timeout<=0) {
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                weakSelf.userInteractionEnabled = YES;
                self.backgroundColor = [UIColor blackColor];
                [weakSelf setTitle:@"发送验证码" forState:UIControlStateNormal];
            });
        }else {
            dispatch_async(dispatch_get_main_queue(), ^{
                self.backgroundColor = ColorRGB(180, 182, 187);
                [weakSelf setTitle:[NSString stringWithFormat:@"重新发送(%d)",timeout] forState:UIControlStateNormal];
            });
        }
    });
    dispatch_resume(timer);
}

- (void)setEdgeInsetsBool:(BOOL)edgeInsetsBool {
    if (!edgeInsetsBool) return;
    //使图片和文字水平居中显示
    self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    //文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
    [self setTitleEdgeInsets:UIEdgeInsetsMake(self.imageView.frame.size.height ,-self.imageView.frame.size.width, 0.0,0.0)];
    //图片距离右边框距离减少图片的宽度，其它不边
    [self setImageEdgeInsets:UIEdgeInsetsMake(0.0, 0.0,0.0, -self.titleLabel.bounds.size.width)];
    LKLog(@"%@",NSStringFromCGRect(self.frame));
}

@end
